/**
 * <p>Title: ServiceZMCE80.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:28:52  张小飞  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.soa.SoaManager;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.ce.domain.ZMCEFaultRecord;

/**
 * <p>Title: ServiceZMCE80.java</p>
 * <p>Description: 故障记录列表</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2012-1-5 下午04:28:52  张小飞  Create
 *</pre>
 */
public class ServiceZMCE80 extends ServiceEPBase {

	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:45:34 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:45:34 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdRecordDeptId", UserSession.getInSessionProperty("orgCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEFaultRecord.query", new ZMCEFaultRecord());
		
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ce.faultType", true));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", true));		
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.people", "empCode", "empName", true));
		
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:45:34 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdCode = this.generateCode(inInfo.getBlock("result").getCellStr(0, "fdEquipmentCode"));
		if("$".equals(fdCode)){
			inInfo.setStatus(-1);
			inInfo.setMsg("编号的最后四位尾码已经超出了编号规则所规定的四位，请调整已有的数据！");
			return inInfo;
		}
		String faultRecordKey = TablePk.getInstance().getNextValue("T_CE_FAULTRECORD");
		inInfo.getBlock("result").setCell(0, "fdKeyId", faultRecordKey);
		inInfo.getBlock("result").setCell(0, "fdFaultreFlag", "0");
		inInfo.getBlock("result").setCell(0, "fdCode", fdCode);
		inInfo.getBlock("result").setCell(0, "fdType", "草拟");
		inInfo.getBlock("result").setCell(0, "fdRecorder", UserSession.getUserId());
		inInfo.getBlock("result").setCell(0, "fdRecordDeptId", UserSession.getInSessionProperty("orgCode"));
		EiInfo outInfo = super.insert(inInfo, "ZMCEFaultRecord.insert");
		
		
		
		return outInfo;
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:45:34 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdRecorder", UserSession.getUserId());
		inInfo.getBlock("result").setCell(0, "fdRecordDeptId", UserSession.getInSessionProperty("orgCode"));
		EiInfo outInfo = super.update(inInfo, "ZMCEFaultRecord.update");
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE80</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-5下午04:45:34 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除异常问题
		for(int i=0; i < inInfo.getBlock("result").getRowCount(); i++){
			inInfo.getBlock("result").setCell(i, "fdWorkrecordId", inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		super.delete(inInfo, "ZMCEUnusualProblem.deleteAfterFaultRecord");
		
		EiInfo _temp = super.delete(inInfo, "ZMCEFaultRecord.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 更新发布状态为"生效"的方法
	 * <p>Function: publish</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	public EiInfo publish(EiInfo inInfo) 
	{
		inInfo.getBlock("result").setCell(0, "fdType", "生效");
		EiInfo _temp = super.update(inInfo, "ZMCEFaultRecord.updateType");
        //改变设备的使用状态
        //同时保存异常情况进度设备异常记录
		inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备故障");
		inInfo.getBlock("result").setCell(0, "fdWorkrecordId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_UNUSUALPROBLEM"));
		inInfo.getBlock("result").setCell(0, "fdUseDepartment", inInfo.getBlock("result").getCellStr(0, "fdEquipmentDepartment"));
		inInfo.getBlock("result").setCell(0, "fdDegree", "一般");
		inInfo.getBlock("result").setCell(0, "fdStatus", inInfo.getBlock("result").getCellStr(0, "fdStatus").toString());
		inInfo.getBlock("result").setCell(0, "fdPerson", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdDatetime", StaticTools.getDateTime());
		super.insert(inInfo, "ZMCEUnusualProblem.insert");
		EiInfo outInfo = this.query(_temp);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: generateCode</p>
	 * <p>Description: 自动生成故障编号</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-9上午10:22:43 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String generateCode(String toencode){
		String code = "";
		List<Map> _list = dao.query("ZMCEFaultRecord.queryLatestCode", null);
		Object fdCode = _list.get(0).get("fdCode");
		if(fdCode == null){
			code = "GZ" + toencode + "0001";
		}else{
			String added = (Integer.parseInt(fdCode.toString())+1) + "";
			if(added.length() == 1){
				code = "GZ" + toencode + "000" + added;
			}else if(added.length() == 2){
				code = "GZ" + toencode + "00" + added;
			}else if(added.length() == 3){
				code = "GZ" + toencode + "0" + added;
			}else if(added.length() == 4){
				code = "GZ" + toencode + added;
			}else{
				code = "$";//此处设置的“$”为一个固定标识,切勿随意修改
			}
		}
		return code;
	}
}
